<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ Utils-ConvertBasicValue Tutorial</title>
        <meta name="description" content="Small Java utilities with many useful functions. Utility methods for converting objects to basic Java types" />
        <meta name="keywords" content="AJ Utils, ajaxjs, ajaxjs framework, java utilities, tools, helper, type conversion,primitive types,Java" />
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" href="https://framework.ajaxjs.com/static/new-ui/css/common.css" />
        <link rel="stylesheet" href="https://iam.ajaxjs.com/asset/main.css"/>
        <link rel="icon" type="image/x-icon" href="https://framework.ajaxjs.com/aj-logo/logo.ico"/>
        <script src="https://framework.ajaxjs.com/static/aj-docs/common.js"></script>
        <script>
            // 获取用户的默认语言
            var userLang = navigator.language || navigator.userLanguage;

            // 检查是否为中文环境（包括简体和繁体）
            if (userLang.startsWith('zh') && location.pathname.indexOf('cn') == -1) {
                 confirm('欢迎！您可以改为访问中文内容。是否继续？') && location.assign('/cn');  // 如果是中文，则弹出提示
            }

            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?208c5aea11c52991bd1c3283e62ad0ce";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();
        </script>
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/">🏠 Home</a>
                    | ⚙️ Source:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-util">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-util">Gitcode</a>
                    | 🚀 <a href="/cn">Chinese Version</a>
                </div>
                <h1>
                    <img src="https://framework.ajaxjs.com/aj-logo/logo.png" style="vertical-align: middle;height: 45px;margin-bottom: 6px;" />
                    AJ Utils
                </h1>
                <h3>Small Java Utils Library</h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/">Home</a>
                    </li>
                </ul>
                <h3>Common</h3>
                <ul>
                    <li><a href="/common/JsonUtil/">JsonUtil</a></li>
                    <li><a href="/common/BytesHelper/">BytesHelper</a></li>
                    <li><a href="/common/ConvertBasicValue/">ConvertBasicValue</a></li>
                    <li><a href="/common/MessageDigestHelper/">HashHelper</a></li>
                    <li><a href="/common/MapTool/">MapTool</a></li>
                    <li><a href="/common/ObjectHelper/">ObjectHelper</a></li>
                    <li><a href="/common/RandomTools/">RandomTools</a></li>
                    <li><a href="/common/RegExpUtils/">RegExpUtils</a></li>
                    <li><a href="/common/StrUtil/">StrUtil</a></li>
                    <li><a href="/common/BoxLogger/">BoxLogger</a></li>
                    <li><a href="/common/BytesHelper/">BytesHelper</a></li>
                    <li><a href="/common/EncodeTools/">UrlEncode</a></li>
                    <li><a href="/common/XmlHelper/">XmlHelper</a></li>
                    <li><a href="/common/WebUtils/">WebUtils</a></li>
                </ul>
                <h3>Date Handing</h3>
                <ul>
                    <li><a href="/date/intro/">Introduction</a></li>
                    <li><a href="/date/convert/">Date Converter</a></li>
                    <li><a href="/date/formatter/">Date Formatter</a></li>
                </ul>
                <h3>Reflection</h3>
                <ul>
                    <li><a href="/reflect/Methods/">Methods</a></li>
                    <li><a href="/reflect/Types/">Types</a></li>
                    <li><a href="/reflect/Clazz/">Clazz</a></li>
                </ul>

                <h3>IO</h3>
                <ul>
                    <li><a href="/io/FileHelper/">FileHelper</a></li>
                    <li><a href="/io/Resources/">Resources</a></li>
                    <li><a href="/io/StreamHelper/">DataReader/DataWriter</a></li>
                    <li><a href="/io/ZipHelper/">ZipHelper</a></li>
                </ul>

                <h3>HTTP Request</h3>
                <ul>
                    <li><a href="/http_request/Get">HTTP Request</a></li>
                    <li><a href="/http_request/advanced-usage/">Advanced Usage</a></li>
                    <li><a href="/http_request/Base/">Base</a></li>
                </ul>
    
               <h3>Cryptography</h3>
               <ul>
                <li><a href="/cryptography/intro/">Introduction</a></li>
                <li><a href="/cryptography/flow/">Basic Flow</a></li>
                <li><a href="/cryptography/AesCrypto/">AES/DES</a></li>
                <li><a href="/cryptography/RsaCrypto/">RSA</a></li>
               </ul>
            </menu>
            <article class="aj-text">
                <h1>ConvertBasicValue Tutorial</h1>
<p>This tutorial provides an overview of the <code>ConvertBasicValue</code> class, which is part of
the <code>lightweight-component/aj-util</code> library. <code>ConvertBasicValue</code> offers a collection of utility methods for converting
objects to
basic Java types. This guide will cover the purpose of each method and provide usage examples.</p>
<h2>Introduction</h2>
<p>The <code>ConvertBasicValue</code> class provides methods for safely converting objects to various basic Java types
like <code>String</code>, <code>boolean</code>, <code>int</code>, <code>long</code>, <code>float</code>, <code>double</code>, <code>Date</code>, and <code>BigDecimal</code>. These methods handle null
values and attempt to parse string representations where appropriate, providing a more robust alternative to direct
casting.</p>
<h2>Methods</h2>
<h3>1. <code>basicCast(Object value, Class&lt;T&gt; clz)</code></h3>
<p>Safely casts an object to a specified class type. This method leverages <code>basicConvert</code> to perform the initial conversion
and then casts the result to the desired type.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>value</code>: The object to convert.</li>
<li><code>clz</code>: The target class type.</li>
</ul>
</li>
<li><strong>Returns:</strong> The converted object of type <code>T</code>.</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-java">Integer intValue = ConvertBasicValue.basicCast(&quot;123&quot;, Integer.class);
// intValue will be 123
</code></pre>
<h3>2. <code>basicConvert(Object value, Class&lt;?&gt; clz)</code></h3>
<p>Converts an object to a specified class type. This method handles <code>null</code> values and performs type-specific conversions.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>value</code>: The object to convert.</li>
<li><code>clz</code>: The target class type.</li>
</ul>
</li>
<li><strong>Returns:</strong> The converted object, or <code>null</code> if the input value is <code>null</code>.</li>
</ul>
<p>This method contains the core logic for different type conversions. Let's look at some of the specific conversions it
handles:</p>
<ul>
<li><strong>String:</strong> Converts the object to a string using <code>value.toString()</code>.</li>
<li><strong>boolean/Boolean:</strong> Converts the object to a boolean using the <code>toBoolean()</code> method (explained below).</li>
<li><strong>int/Integer:</strong> Converts the object to an integer using the <code>object2int()</code> method (explained below).</li>
<li><strong>long/Long:</strong> Converts the object to a long using the <code>object2long()</code> method (explained below).</li>
<li><strong>float/Float:</strong> Converts the object to a float using the <code>object2float()</code> method.</li>
<li><strong>double/Double:</strong> Converts the object to a double using the <code>object2double()</code> method.</li>
<li><strong>Date:</strong> Converts the object to a Date using the <code>DateHelper.object2Date()</code> method.</li>
<li><strong>BigDecimal:</strong> Converts the object to a BigDecimal if the value is a String or Number.</li>
<li><strong>Array:</strong> Converts the object to an array using the <code>toArray()</code> method.</li>
<li><strong>Enum:</strong> Converts the object to an enum.</li>
</ul>
<h3>3. <code>toBoolean(Object value)</code></h3>
<p>Converts an object to a boolean. This method handles various input types, including strings, numbers, and booleans.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>value</code>: The object to convert.</li>
</ul>
</li>
<li><strong>Returns:</strong> The boolean value of the object.</li>
</ul>
<p><strong>Examples:</strong></p>
<pre><code class="language-java">assertTrue(ConvertBasicValue.toBoolean(true));
assertTrue(ConvertBasicValue.toBoolean(&quot;true&quot;));
assertTrue(ConvertBasicValue.toBoolean(&quot;1&quot;));
assertFalse(ConvertBasicValue.toBoolean(&quot;false&quot;));
assertFalse(ConvertBasicValue.toBoolean(0));
</code></pre>
<h3>4. <code>object2int(Object value)</code></h3>
<p>Converts an object to an integer. This method handles <code>null</code> values and attempts to parse string representations.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>value</code>: The object to convert.</li>
</ul>
</li>
<li><strong>Returns:</strong> The integer value of the object, or 0 if the input value is <code>null</code>.</li>
<li><strong>Throws:</strong> <code>IllegalArgumentException</code> if the object cannot be converted to an integer.</li>
</ul>
<p><strong>Examples:</strong></p>
<pre><code class="language-java">assertEquals(0, ConvertBasicValue.object2int(null));
assertEquals(123, ConvertBasicValue.object2int(&quot;123&quot;));
assertEquals(-456, ConvertBasicValue.object2int(&quot;-456&quot;));
</code></pre>
<h3>5. <code>object2long(Object value)</code></h3>
<p>Converts an object to a long. This method handles <code>null</code> values and attempts to parse string representations.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>value</code>: The object to convert.</li>
</ul>
</li>
<li><strong>Returns:</strong> The long value of the object, or 0L if the input value is <code>null</code>.</li>
<li><strong>Throws:</strong> <code>IllegalArgumentException</code> if the object cannot be converted to a long.</li>
</ul>
<p><strong>Examples:</strong></p>
<pre><code class="language-java">assertEquals(0L, ConvertBasicValue.object2long(null));
assertEquals(123L, ConvertBasicValue.object2long(&quot;123&quot;));
assertEquals(-456L, ConvertBasicValue.object2long(&quot;-456&quot;));
</code></pre>
<h3>6. <code>object2double(Object value)</code></h3>
<p>Converts an object to a double. This method handles <code>null</code> values and attempts to parse string representations.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>value</code>: The object to convert.</li>
</ul>
</li>
<li><strong>Returns:</strong> The double value of the object, or 0.0 if the input value is <code>null</code>.</li>
</ul>
<p><strong>Examples:</strong></p>
<pre><code class="language-java">assertEquals(0.0, ConvertBasicValue.object2double(null), 0.0001);
assertEquals(3.14, ConvertBasicValue.object2double(&quot;3.14&quot;), 0.0001);
</code></pre>
<h2>Unit Test Examples</h2>
<p>Here are some examples from the unit tests:</p>
<pre><code class="language-java">import org.junit.jupiter.api.Test;
import static com.ajaxjs.util.ConvertBasicValue.*;
import static org.junit.jupiter.api.Assertions.*;

public class TestConvertBasicValue {
    @Test
    public void testToBoolean() {
        assertTrue(toBoolean(true));
        assertTrue(toBoolean(&quot;true&quot;));
        assertTrue(toBoolean(&quot;True&quot;));

        assertFalse(toBoolean(&quot;fAlse&quot;));
        assertFalse(toBoolean(&quot;null&quot;));

        assertTrue(toBoolean(&quot;on&quot;));
        assertTrue(toBoolean(&quot;yes&quot;));
        assertTrue(toBoolean(1));
        assertTrue(toBoolean(&quot;1&quot;));
    }

    @Test
    public void testObject2Int() {
        assertEquals(0, object2int(null));
        assertEquals(100, object2int(100));
        assertEquals(123, object2int(&quot;123&quot;));
    }

    @Test
    public void testObject2Long() {
        assertEquals(0L, object2long(null));
        assertEquals(100L, object2long(100L));
        assertEquals(123L, object2long(&quot;123&quot;));
    }

    @Test
    public void testObject2Double() {
        assertEquals(0.0, object2double(null), 0.0001);
        assertEquals(3.14, object2double(3.14), 0.0001);
    }
}
</code></pre>
<h2>Conclusion</h2>
<p>The <code>ConvertBasicValue</code> class provides a useful set of utilities for safely converting objects to basic Java types. By
using these methods, you can simplify your code and handle potential <code>null</code> values and parsing errors
more gracefully. Remember to consult the library's Javadoc for the most up-to-date information and additional methods.</p>

            </article>
        </div>

       <footer>
            AJ-Util, a part of <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> open source. Mail:
            frank@ajaxjs.com, visit <a href="https://blog.csdn.net/zhangxin09" target="_blank">my blog(In Chinese)</a>.
            <br />
            <br />
            Copyright © 2025 Frank Cheung. All rights reserved.
        </footer>
    </body>
</html>